// RemoteMapperDlg.h : header file
//
#pragma once

#include "customcontrol/Picture.h"
#include "customcontrol/KbcButton.h"
#include "customcontrol/TextStatic.h"
#include "win32utility/RemoteEmuManager.h"
#include "MyEditControl.h"
#include "trayicon.h"
#include "MsgShow.h"
#include "IRemoteAdapter.h"

using TM::E_REMOTEKEY_COUNT;

namespace Utility{
	namespace Configuration{
		class CConfigurationUtil;
	}
}

namespace RemoteUtility{
	namespace RemoteAdapter{
		class IRemoteAdapter;
	}
}

using TM::Win32Utility::RemoteUtility::CRemoteEmuManager;
using TM::RemoteMap::IFocusTest;

// CRemoteMapperDlg dialog
class CRemoteMapperDlg : public CDialog, public IFocusTest
{
	enum {
		E_CLIENTAREA_WIDTH = 800,
		E_CLIENTAREA_HEIGHT = 600,
		E_TITLEBAR_HEIGHT = 0,

		E_CHECKING_INTERVAL = 500,
		E_VIDEOPLAY_ELAPSE = 1000,
		E_VIDEOPLAY_TIMER = 9999,
		E_INVALID_PID = -1,

		E_NOTIFYCODE_NOERROR = 1,
		E_NOTIFYCODE_BROKEN = 2,
		E_NOTIFYCODE_F2PRESSED = 3,
		E_NOTIFYCODE_F3PRESSED = 4,
		E_NOTIFYCODE_F4PRESSED = 5,
	};
	enum {
		E_KEYMAP_UP = 0,
		E_KEYMAP_DOWN,
		E_KEYMAP_LEFT,
		E_KEYMAP_RIGHT,
		E_KEYMAP_OK,
		E_KEYMAP_HOME,
		E_KEYMAP_POWER,
		E_KEYMAP_SQUARE,
		E_KEYMAP_CIRCLE,
		E_KEYMAP_TRIG,

		E_KEYMAP_COUNT,
	};

	enum {
		E_PATTERNMAP_UP = 0,
		E_PATTERNMAP_DOWN,
		E_PATTERNMAP_LEFT,
		E_PATTERNMAP_RIGHT,
		E_PATTERNMAP_THRESH,
		E_PATTERNMAP_DITHER,

		E_PATTERNMAP_COUNT,
	};

	typedef enum {
		E_KEYEVENT_ENTER = 0,
		E_KEYEVENT_ESCAPE,
		E_KEYEVENT_LEFT,
		E_KEYEVENT_RIGHT,

		E_KEYEVENT_COUNT
	} EKeyeventType;
private:
	typedef std::bitset<E_KEYEVENT_COUNT> TKeyBits;
	TKeyBits m_keyBits;

// Construction
public:
	CRemoteMapperDlg(CWnd* pParent = NULL);	// standard constructor
	virtual ~CRemoteMapperDlg();	

// Dialog Data
	enum { IDD = IDD_REMOTEMAPPER_DIALOG };

	virtual bool testFocus() const;

protected:
	virtual void DoDataExchange(CDataExchange* pDX);	// DDX/DDV support
	void initButton();

	HICON m_hIcon;

	// Generated message map functions
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	afx_msg HCURSOR OnQueryDragIcon();

	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
	afx_msg BOOL OnEraseBkgnd(CDC* pDC);	
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

	//three button
	afx_msg void OnBnClickedBtnClose();
	afx_msg void OnBnClickedBtnStart();
	afx_msg void OnBnClickedBtnStop();

	LRESULT OnKeyCodeChanged(WPARAM pa1, LPARAM pa2);
	//map key number to string
	CString mapKeyNumberToString(int key);
	//map key string to number
	int mapKeyStringToNumber(CString key);

	//////////////////////////////////////////////////////////////////////////
	// Tray icon functions
	afx_msg LRESULT OnTrayNotification( WPARAM uID, LPARAM lEvent );
	afx_msg LRESULT OnRestoreFromTray( WPARAM uID, LPARAM lEvent );
	afx_msg void OnAppShow();
	afx_msg void OnAppExit();
	afx_msg LRESULT OnWindowShow( WPARAM, LPARAM );
	afx_msg LRESULT OnWindowHide( WPARAM, LPARAM );
	afx_msg LRESULT OnRemoteNotify( WPARAM, LPARAM );
	afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

	//
	BOOL PreTranslateMessage(MSG* pMsg);
	void OnKeyEvent( EKeyeventType ekt, BOOL bDown );
	void OnMouseEvent();

	//load from cfg file for the default config
	void loadPreviousConfigDescription(LPCSTR strMapCfg, LPCSTR strMsidCfg);
	//save to file
	void saveToFile( LPCSTR strMapCfg, LPCSTR strMsidCfg );
	void updateRemoteStatus();

	//remote emulation, for display remote status real time
	void startRemoteNotify();
	void stopRemoteEmulation();
	void startCheckEmulation();
	void stopCheckEmulation();

	DECLARE_MESSAGE_MAP()

	//background
	CPicture m_picGameInfo;
	CPicture m_picBgBack;
	CPicture m_picLogo;

	//config instance
	Utility::Configuration::CConfigurationUtil* m_pConfig;

	//all edit control here
	CMyEditCtrl m_editKeyMap[E_KEYMAP_COUNT];
	CMyEditCtrl m_editPatMap[E_PATTERNMAP_COUNT];

	//buttons
	CKbcButton m_btnStartSimulate, m_btnStopSimulate, m_btnClose;

	// Windows tray icon control.
	CTrayIcon m_trayIcon;

	//
	CRemoteEmuManager* m_pRemoteEmuManager;

	CMsgShow m_msgShow;
	RemoteUtility::RemoteAdapter::IRemoteAdapter* m_remoteAdapter;

	//remote status message
	RemoteUtility::RemoteAdapter::E_ZMotion_ErrorCode m_remoteErrorCode;
	CTextStatic m_infoRemoteStatus;
};
